package com.colodoo.code.action;

import com.colodoo.code.common.PageInfo;
import com.colodoo.code.common.QueryRequest;
import com.colodoo.code.entity.ModuleInf;
import com.colodoo.code.service.ModuleInfService;
import com.colodoo.framework.common.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * 模块控制器
 *
 * @author zhisan
 */
@RestController
@RequestMapping("/code/moduleInf")
@Slf4j
public class ModuleInfAction {

	@Resource
	ModuleInfService moduleInfService;

	/**
	 * 新增数据
	 *
	 * @param moduleInf
	 * @return
	 */
	@PostMapping
	public Result saveModuleInf(@RequestBody ModuleInf moduleInf) {
		this.moduleInfService.saveModuleInf(moduleInf);
		return Result.ok();
	}

	/**
	 * 根据ID删除数据
	 *
	 * @param moduleInfId
	 * @return
	 */
	@DeleteMapping("/{moduleInfId}")
	public Result deleteModuleInfById(@PathVariable(name = "moduleInfId") Integer moduleInfId) {
		this.moduleInfService.deleteModuleInfById(moduleInfId);
		return Result.ok();
	}

	/**
	 * 更新数据
	 *
	 * @param moduleInfId
	 * @return
	 */
	@PutMapping("/{moduleInfId}")
	public Result updateModuleInf(@PathVariable(name = "moduleInfId") Integer moduleInfId, @RequestBody ModuleInf moduleInf) {
		this.moduleInfService.updateModuleInf(moduleInf);
		return Result.ok();
	}

	/**
	 * 查询数据
	 *
	 * @param moduleInf
	 * @return
	 */
	@GetMapping
	public Result getModuleInfs(ModuleInf moduleInf, QueryRequest request) {
		if (request.getPage() == null) {
			return Result.ok(this.moduleInfService.getModuleInfs(moduleInf));
		}
		Page<ModuleInf> moduleInfPage = this.moduleInfService.getModuleInfsByPage(moduleInf, request);
		return Result.ok(new PageInfo<ModuleInf>(moduleInfPage.getTotalElements(), moduleInfPage.getContent()));
	}


}
